package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ISatelliteWeatherService;

/**
 * 卫星气象数据控制器
 */
@RestController
@RequestMapping("/system/satellite")
public class SatelliteWeatherController extends BaseController {

    @Autowired
    private ISatelliteWeatherService satelliteWeatherService;

    /**
     * 获取实时气象数据
     */
    @GetMapping("/weather/realtime")
    public AjaxResult getRealTimeWeather(String location) {
        try {
            if (location == null || location.trim().isEmpty()) {
                location = "默认农田区域";
            }
            Map<String, Object> weatherData = satelliteWeatherService.getRealTimeWeather(location);
            return AjaxResult.success(weatherData);
        } catch (Exception e) {
            return AjaxResult.error("获取气象数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取灾害预警信息
     */
    @GetMapping("/disaster/alerts")
    public AjaxResult getDisasterAlerts() {
        try {
            List<Map<String, Object>> alerts = satelliteWeatherService.getDisasterAlerts();
            return AjaxResult.success(alerts);
        } catch (Exception e) {
            return AjaxResult.error("获取灾害预警失败: " + e.getMessage());
        }
    }

    /**
     * 同步卫星数据
     */
    @GetMapping("/sync")
    public AjaxResult syncSatelliteData(String location) {
        try {
            if (location == null || location.trim().isEmpty()) {
                location = "默认农田区域";
            }
            boolean success = satelliteWeatherService.syncSatelliteData(location);
            return success ? AjaxResult.success("数据同步成功") : AjaxResult.error("数据同步失败");
        } catch (Exception e) {
            return AjaxResult.error("数据同步失败: " + e.getMessage());
        }
    }

    /**
     * 获取气象统计数据
     */
    @GetMapping("/weather/stats")
    public AjaxResult getWeatherStats(String location) {
        try {
            if (location == null || location.trim().isEmpty()) {
                location = "默认农田区域";
            }
            Map<String, Object> stats = satelliteWeatherService.getWeatherStats(location);
            return AjaxResult.success(stats);
        } catch (Exception e) {
            return AjaxResult.error("获取统计数据失败: " + e.getMessage());
        }
    }
}
